﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;

namespace ULog.Common
{


    public class ResultData<T> : IHttpActionResult
    {
        /// <summary>
        /// 构造
        /// </summary>
        public ResultData()
        {
            message = "ok";
            status = 0;
        }

        /// <summary>
        /// 错误信息
        /// </summary>
        public string message { set; get; }

        /// <summary>
        /// 返回状态
        /// 0：成功,否则失败
        /// </summary>
        public int status { set; get; }

        /// <summary>
        /// 返回信息
        /// </summary>
        public T data { set; get; }

        public static ResultData<T> Success(T _data)
        {
            ResultData<T> result = new ResultData<T>
            {
                status =0,
                message = string.Empty,
                data = _data
            };
            return result;
        }
        public static ResultData<T> Success(string msg)
        {
            ResultData<T> result = new ResultData<T>
            {
                status = 0,
                message = msg,
            };
            return result;
        }
        public static ResultData<T> Error(string errMsg)
        {
            ResultData<T> result = new ResultData<T>
            {
                status = -1,
                message = errMsg
            };
            return result;
        }

        public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
        {
            var response = new HttpResponseMessage()
            {
                Content = new ObjectContent(typeof(object), this, new JsonMediaTypeFormatter()),
            };
            return Task.FromResult(response);
        }
    }
}